package javase.CollectionAndMap.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class ArrayListSortDemo {

    public static void main(String[] args) {
        System.out.println("--------------- ArrayList<String> --------------");
        List<String> list = new ArrayList<>();
        list.add("aba");
        list.add("aed");
        list.add("ade");
        list.add("add");
        //排序  传入比较器
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //升序
                return o1.compareTo(o2);
            }
        });
        System.out.println("排序后："+list);

        System.out.println("--------------- ArrayList<Employee>  sort() --------------");
        List<Employee> empList = new ArrayList<>();
        empList.add(new Employee("zhangsan" , 8000.0));
        empList.add(new Employee("lisi" , 8000.0));
        empList.add(new Employee("zhangsan" , 6000.0));
        empList.add(new Employee("wangwu" , 9000.0));
        //排序  传入比较器
        /*empList.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                if(o1.getName().equals(o2.getName())){
                    //升序
                    return o1.getSalary().compareTo(o2.getSalary());
                }
                return o1.getName().compareTo(o2.getName());
            }
        });
        System.out.println("排序后："+empList);*/

        System.out.println("--------------- Collections  sort(list) --------------");
        //排序  元素自身具备比较性
        Collections.sort(empList);
        System.out.println("排序后："+empList);
    }

}
class Employee implements Comparable<Employee> {
    private String name;
    private Double salary;

    //元素自身具备比较性,比较逻辑
    @Override
    public int compareTo(Employee o) {
        if(this.name.equals(o.name)){
            //降序
            return o.salary.compareTo(this.salary);
        }
        return this.name.compareTo(o.name);
    }

    public Employee() {
    }

    public Employee(String name, Double salary) {
        this.name = name;
        this.salary = salary;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return salary
     */
    public Double getSalary() {
        return salary;
    }

    /**
     * 设置
     * @param salary
     */
    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public String toString() {
        return "Employee{name = " + name + ", salary = " + salary + "}";
    }


}
